#include <QtGui>

#include "Werteditor.h"

Werteditor::Werteditor(QObject *eltern) :  QStyledItemDelegate(eltern)
{
}
QString Werteditor::displayText(const QVariant &wert, const QLocale &sprache) const
{
  return QString("%1%2").arg(QLocale().toString(wert.toDouble(),'f',2)).arg(sprache.currencySymbol());
}
QWidget* Werteditor::createEditor(QWidget *eltern, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  if(!index.isValid())
    return QStyledItemDelegate::createEditor(eltern,option,index);
  QDoubleSpinBox* Editor=new QDoubleSpinBox(eltern);
  Editor->setButtonSymbols(QAbstractSpinBox::NoButtons);
  Editor->setSuffix(QLocale().currencySymbol());
  Editor->setMinimum(-100000000.00);
  return Editor;
}
void Werteditor::setEditorData(QWidget *editor, const QModelIndex &index) const
{
  QDoubleSpinBox* Editor=qobject_cast<QDoubleSpinBox *>(editor);
  double Wert=index.data().toDouble();
  static QPalette rot;
  static QPalette normal;
  rot.setColor(QPalette::Text,Qt::red);
  QPalette Palette;
  if(Wert>=0)
    Palette=normal;
  else
    Palette=rot;
  Editor->setPalette(Palette);
  Editor->setValue(Wert);
}
void Werteditor::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
  QDoubleSpinBox* Editor=qobject_cast<QDoubleSpinBox *>(editor);
  model->setData(index,Editor->value());
}
